package com.luochen.cdpt.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.luochen.cdpt.repository.IUserRepository;
import com.luochen.cdpt.eneity.User;

@Service
public class UserService {

	@Resource
	private IUserRepository userRepository;

	public List<User> findUserList(Map<String, Object> params) {
		List<User> list = userRepository.findAll(new Specification<User>() {
			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				List<Predicate> predicates = new ArrayList<Predicate>();
				if (params.get("name_like") != null && !StringUtils.isEmpty((String) params.get("name_like"))) {
					predicates.add(cb.like(root.get("username").as(String.class),
							"%" + (String) params.get("name_like") + "%"));
				}
				return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
			}
		});
		return list;
	}

	public Page<User> findUserListByPage(Map<String, Object> params) {
		Pageable pageRequest = PageRequest.of(0, 20);
		Page<User> list = userRepository.findAll(new Specification<User>() {
			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				List<Predicate> predicates = new ArrayList<Predicate>();
				if (params.get("name_like") != null && !StringUtils.isEmpty((String) params.get("name_like"))) {
					predicates.add(cb.like(root.get("username").as(String.class),
							"%" + (String) params.get("name_like") + "%"));
				}
				return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
			}
		}, pageRequest);
		return list;
	}



	//	@Override
//	public List<Student> getStudent(String studentNumber,String name ,String nickName,
//									Date birthday,String courseName,float chineseScore,float mathScore,
//									float englishScore,float performancePoints) {
//		Specification<Student> specification = new Specification<Student>(){
//
//			@Override
//			public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//				//用于暂时存放查询条件的集合
//				List<Predicate> predicatesList = new ArrayList<>();
//				//--------------------------------------------
//				//查询条件示例
//				//equal示例
//				if (!StringUtils.isEmpty(name)){
//					Predicate namePredicate = cb.equal(root.get("name"), name);
//					predicatesList.add(namePredicate);
//				}
//				//like示例
//				if (!StringUtils.isEmpty(nickName)){
//					Predicate nickNamePredicate = cb.like(root.get("nickName"), '%'+nickName+'%');
//					predicatesList.add(nickNamePredicate);
//				}
//				//between示例
//				if (birthday != null) {
//					Predicate birthdayPredicate = cb.between(root.get("birthday"), birthday, new Date());
//					predicatesList.add(birthdayPredicate);
//				}
//
//				//关联表查询示例
//				if (!StringUtils.isEmpty(courseName)) {
//					Join<Student,Teacher> joinTeacher = root.join("teachers",JoinType.LEFT);
//					Predicate coursePredicate = cb.equal(joinTeacher.get("courseName"), courseName);
//					predicatesList.add(coursePredicate);
//				}
//
//				//复杂条件组合示例
//				if (chineseScore!=0 && mathScore!=0 && englishScore!=0 && performancePoints!=0) {
//					Join<Student,Examination> joinExam = root.join("exams",JoinType.LEFT);
//					Predicate predicateExamChinese = cb.ge(joinExam.get("chineseScore"),chineseScore);
//					Predicate predicateExamMath = cb.ge(joinExam.get("mathScore"),mathScore);
//					Predicate predicateExamEnglish = cb.ge(joinExam.get("englishScore"),englishScore);
//					Predicate predicateExamPerformance = cb.ge(joinExam.get("performancePoints"),performancePoints);
//					//组合
//					Predicate predicateExam = cb.or(predicateExamChinese,predicateExamEnglish,predicateExamMath);
//					Predicate predicateExamAll = cb.and(predicateExamPerformance,predicateExam);
//					predicatesList.add(predicateExamAll);
//				}
//				//--------------------------------------------
//				//排序示例(先根据学号排序，后根据姓名排序)
//				query.orderBy(cb.asc(root.get("studentNumber")),cb.asc(root.get("name")));
//				//--------------------------------------------
//				//最终将查询条件拼好然后return
//				Predicate[] predicates = new Predicate[predicatesList.size()];
//				return cb.and(predicatesList.toArray(predicates));
//			}
//
//
//		};
//		return repository.findAll(specification);
//	}
//}

}
